package cn.tedu.egaddress1.controller;

import cn.tedu.egaddress1.base.response.JsonResult;
import cn.tedu.egaddress1.base.response.StatusCode;
import cn.tedu.egaddress1.mapper.AddressMapper;
import cn.tedu.egaddress1.pojo.dto.AddressAddParam;
import cn.tedu.egaddress1.pojo.entity.Address;
import cn.tedu.egaddress1.pojo.vo.AddressInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitConnectionDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/address")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    /**
     *1.添加收货地址功能
     */
    @RequestMapping("/add")
    public JsonResult add(@RequestBody AddressAddParam addressAddParam){
        log.debug("addressAddParam="+addressAddParam);
        Address address = new Address();
        BeanUtils.copyProperties(addressAddParam, address);
        int num = addressMapper.insertAddress(address);
        if(num>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    /**
     * 2.查询收货地址列表功能
     */
    @GetMapping ("/list")
    public JsonResult list(){
        List<AddressInfoVO> addressInfoVO = addressMapper.selectAddressList();
        return JsonResult.ok(addressInfoVO);
    }

    /**
     * 3.删除收货地址功能
     */
    @PostMapping("/del")
    public JsonResult del(Integer id){
        log.debug("id="+id);
        int num = addressMapper.deleteById(id);
        if(num>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
